package top.vanchor.myblog2back.controller.user;


import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import top.vanchor.myblog2back.entity.BlogComment;
import top.vanchor.myblog2back.service.IBlogCommentService;

import javax.annotation.Resource;
import java.beans.beancontext.BeanContextServices;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ding
 * @since 2021-11-12
 */
@CrossOrigin
@RestController
@RequestMapping("/blogComment")
public class BlogCommentController {

    @Resource
    IBlogCommentService iBlogCommentService;

    @ApiOperation("根据博客id获取评论")
    @RequestMapping("/getCommentTreeById")
    @ResponseBody
    public JSONObject getCommentTreeByBlogId(@RequestBody JSONObject data){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",iBlogCommentService.getCommentTreeByBlogId(Integer.parseInt(String.valueOf(data.get("blogId"))),Integer.parseInt(String.valueOf(data.get("uId")))));
        return jsonObject;
    }

    @ApiOperation("发表评论")
    @RequestMapping("/toPublishComment")
    @ResponseBody
    public JSONObject toPublishComment(@RequestBody JSONObject dt){
        JSONObject data = dt.getJSONObject("form");
        JSONObject jsonObject = new JSONObject();
        Integer blogId = Integer.parseInt(String.valueOf(data.get("blog_id")));
        Integer userId = Integer.parseInt(String.valueOf(data.get("user_id")));
        String commentContent = String.valueOf(data.get("commentMsg"));
        Integer replyId = Integer.parseInt(String.valueOf(data.get("replyId")));
        BlogComment blogComment = new BlogComment();
        if (replyId.equals(-1)){
            //顶级评论
            blogComment.setCommentPid(0);
        }else{
            //次级评论
            blogComment.setCommentPid(replyId);
        }
        blogComment.setCommentContent(commentContent);
        blogComment.setCommentDatetime(LocalDateTime.now());
        blogComment.setCommentUserId(userId);
        blogComment.setCommentBlogId(blogId);
        if(iBlogCommentService.toPublishComment(blogComment)>0){
            jsonObject.put("code",200);
            JSONObject jsonDemo = new JSONObject();
            jsonDemo.put("comments",iBlogCommentService.getCommentTreeByBlogId(blogId,-1));
            jsonDemo.put("commentNum",iBlogCommentService.getCommentNumByBlogId(blogId));
            jsonObject.put("data",jsonDemo);
        }else{
            jsonObject.put("code",500);
        }
        return jsonObject;
    }

    @ApiOperation("根据id删除评论")
    @RequestMapping("/toDelCommentById")
    @ResponseBody
    public JSONObject toDelCommentById(@RequestBody JSONObject data){
        JSONObject jsonObject = new JSONObject();
        if (iBlogCommentService.toDelCommentById(Integer.parseInt(String.valueOf(data.get("comment_id")))) > 0){
            jsonObject.put("code",200);
            JSONObject jsonDemo = new JSONObject();
            jsonDemo.put("comments",iBlogCommentService.getCommentTreeByBlogId(Integer.parseInt(String.valueOf(data.get("blog_id"))),Integer.parseInt(String.valueOf(data.get("uId")))));
            jsonObject.put("data",jsonDemo);
        }else{
            jsonObject.put("code",500);
            jsonObject.put("msg","服务器异常，操作失败！");
        }
        return jsonObject;
    }

    @ApiOperation("点赞/取赞评论")
    @RequestMapping("/toLikeComment")
    @ResponseBody
    public JSONObject toLikeComment(@RequestBody JSONObject data){
        JSONObject jsonObject = new JSONObject();
        if (iBlogCommentService.toLikeComment(Integer.parseInt(String.valueOf(data.get("commentId"))),Integer.parseInt(String.valueOf(data.get("uId"))),Integer.parseInt(String.valueOf(data.get("type")))) > 0){
            jsonObject.put("code",200);
            JSONObject jsonDemo = new JSONObject();
            jsonObject.put("data",jsonDemo);
        }else{
            jsonObject.put("code",500);
            jsonObject.put("msg","服务器异常，操作失败！");
        }
        return jsonObject;
    }


}
